package org.darcy.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.darcy.dao.SysLinkMapper;
import org.darcy.entity.SysLink;
import org.darcy.framework.blog.business.enums.ConfigKeyEnum;
import org.darcy.framework.blog.business.enums.LinkSourceEnum;
import org.darcy.framework.blog.business.enums.TemplateKeyEnum;
import org.darcy.framework.blog.business.util.LinksUtil;
import org.darcy.framework.blog.exception.BlogLinkException;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.util.HtmlUtil;
import org.darcy.framework.util.RegexUtils;
import org.darcy.service.MailService;
import org.darcy.service.SysConfigService;
import org.darcy.service.SysLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;

/**
 * 友情链接
 */
@Slf4j
@Service
public class SysLinkServiceImpl extends GenericServiceImpl<SysLink> implements SysLinkService {

	@Autowired
	private SysLinkMapper sysLinkMapper;
	@Autowired
	private MailService mailService;
	@Autowired
	private SysConfigService configService;

	/**
	 * 根据位置、状态查询友情链接
	 */
	private List<SysLink> getList(Integer type, Integer status) {
		QueryWrapper<SysLink> q = new QueryWrapper<SysLink>();
		q.eq("status", status);
		if (type != null) {
			q.eq("home_page_display", type);
		}
		List<SysLink> list = super.getList(q);
		return list;
	}

	/**
	 * 查询可在首页显示的友情链接列表
	 *
	 * @return
	 */
	@Override
	public List<SysLink> listOfIndex() {
		return getList(1, 1);
	}

	/**
	 * 查询可在内页显示的友情链接列表
	 *
	 * @return
	 */
	@Override
	public List<SysLink> listOfInside() {
		return getList(0, 1);
	}

	/**
	 * 查询已禁用的友情链接列表
	 *
	 * @return
	 */
	@Override
	public List<SysLink> listOfDisable() {
		return getList(null, 0);
	}

	/**
	 * 分组获取所有连接 {indexList:首页显示,insideList:内页,disableList:禁用}
	 *
	 * @return
	 */
	@Override
	public Map<String, List<SysLink>> listAllByGroup() {
		// 首页连接
		List<SysLink> listOfIndex = this.listOfIndex();
		// 内页连接
		List<SysLink> listOfInside = this.listOfInside();
		// 已禁用的连接
		List<SysLink> listOfDisable = this.listOfDisable();
		Map<String, List<SysLink>> resultMap = new HashMap<>();
		resultMap.put("indexList", listOfIndex);
		resultMap.put("insideList", listOfInside);
		resultMap.put("disableList", listOfDisable);
		return resultMap;
	}

	/**
	 * 自动添加友链
	 *
	 * @param link
	 * @return
	 */
	@Override
	public boolean autoLink(SysLink link) throws BlogLinkException {
		String url = link.getUrl();
		if (StringUtils.isEmpty(url)) {
			throw new BlogLinkException("链接地址为空！");
		}
		if (!RegexUtils.isUrl(url)) {
			throw new BlogLinkException("链接地址无效！");
		}
		SysLink bo = getOneByUrl(url);
		if (bo != null) {
			throw new BlogLinkException("本站已经添加过贵站的链接！");
		}
		Map config = configService.getConfigs();
		String domain = (String) config.get(ConfigKeyEnum.DOMAIN.getKey());
		if (!(LinksUtil.hasLinkByHtml(url, domain)) && !LinksUtil.hasLinkByChinaz(url, domain)) {
			throw new BlogLinkException("贵站暂未添加本站友情链接！请先添加本站友链后重新提交申请！");
		}

		link.setSource(LinkSourceEnum.AUTOMATIC.name());
		link.setStatus((byte) 1);
		if (!StringUtils.isEmpty(link.getEmail())) {
			link.setEmail(HtmlUtil.html2Text(link.getEmail()));
		}
		if (!StringUtils.isEmpty(link.getFavicon())) {
			link.setFavicon(HtmlUtil.html2Text(link.getFavicon()));
		}
		if (!StringUtils.isEmpty(link.getName())) {
			link.setName(HtmlUtil.html2Text(link.getName()));
		}
		if (!StringUtils.isEmpty(link.getUrl())) {
			link.setUrl(HtmlUtil.html2Text(link.getUrl()));
		}
		if (!StringUtils.isEmpty(link.getDescription())) {
			link.setDescription(HtmlUtil.html2Text(link.getDescription()));
		}
		this.insert(link);
		log.info("友联自动申请成功,开始发送邮件通知...");
		mailService.send(link, TemplateKeyEnum.TM_LINKS);
		return true;
	}

	@Override
	public SysLink getOneByUrl(String url) {
		QueryWrapper<SysLink> q = new QueryWrapper<SysLink>();
		q.eq("url", url);
		SysLink data = sysLinkMapper.selectOne(q);
		return data;
	}
}
